package rd.system.toeicphoto {
	
	import com.greensock.TweenLite;
	import com.sff.module.Module;
	import com.sff.site.core.INode;
	import com.sff.site.events.NodeEvent;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import rd.system.mp3.MP3;
	import rd.system.mp3.MP3_FC;
	
	import rd.system.loading.Loading_FC;
	
	/**
	 * ...
	 * @author 
	 */
	public class ToeicPhoto extends Module {
		
		public var txtQuestion:TextField;
		public var txtAnswer:TextField;
		public var mcCategory:Category;
		
		private var arrCategory:Array = [];
		private var curCategory:CategoryData;
		private var arrQuestion:Array;
		private var curQuestion:int = 0;
		private var questionData:QuestionData;
		private var curAnswer:MovieClip;
		
		private var ldrImage:Loader;
		private var loading_FC:MovieClip;;
		private var mp3_FC:MP3;
		
		public function ToeicPhoto() {
			
		}
		
		override protected function run():void {
			
			this.system.resizeManager.width = 1024;
			this.system.resizeManager.height = 600;
			
			var xmlCategory:XMLList = this.system.dataManager.xmlData.questions.category;
			for (var i:int = 0; i < xmlCategory.length(); i ++) {
				arrCategory.push(new CategoryData(xmlCategory[i]));
			}
			
			txtQuestion.text = this.system.dataManager.xmlData.labels.label.(@id == 'questionLabel').toString();
			this.system.styleManager.applyStyle(txtQuestion, 'questionLabelStyle');
			
			txtAnswer.text = this.system.dataManager.xmlData.labels.label.(@id == 'anwserLabel').toString();
			this.system.styleManager.applyStyle(txtAnswer, 'answerLabelStyle');
			
			loading_FC = new Loading_FC();
			loading_FC.x = mcImage.mcBG.width / 2;
			loading_FC.y = mcImage.mcBG.height / 2;
			loading_FC.stop();
			loading_FC.visible = false;
			mcImage.addChild(loading_FC);
			
			btnValidate.txtLabel.autoSize = TextFieldAutoSize.LEFT;
			btnValidate.txtLabel.text = this.system.dataManager.xmlData.labels.label.(@id == 'answerValidate').toString();
			btnValidate.txtLabel.x = 5;
			btnValidate.mcBG.width = btnValidate.txtLabel.x + btnValidate.txtLabel.textWidth + btnValidate.txtLabel.x + 5;
			btnValidate.buttonMode = true;
			btnValidate.mouseChildren = false;
			btnValidate.addEventListener(MouseEvent.CLICK, onClickValidateAnswerHandler);
			
			btnNext.txtLabel.autoSize = TextFieldAutoSize.LEFT;
			btnNext.txtLabel.text = this.system.dataManager.xmlData.labels.label.(@id == 'nextQuestion').toString();
			btnNext.txtLabel.x = 5;
			btnNext.mcBG.width = btnNext.txtLabel.x + btnNext.txtLabel.textWidth + btnNext.txtLabel.x + 5;
			btnNext.buttonMode = true;
			btnNext.mouseChildren = false;
			btnNext.addEventListener(MouseEvent.CLICK, onClickNextQuestionHandler);
			
			mcCategory.setData(this.system.siteManager, arrCategory);
			
			this.system.siteManager.addEventListener(NodeEvent.CHILD_CHANGE, onChildChangeHandler);
			onChildChangeHandler(null);			
		}
		
		private function getCategoryByNodeId(strId:String):CategoryData {
			for (var i:int = 0; i < arrCategory.length; i ++) {					
				if (CategoryData(arrCategory[i]).nodeid == strId) {
					return arrCategory[i];
				}
			}
			return null;
		}
		
		private function onChildChangeHandler(evt:NodeEvent):void {
			
			var activeNode:INode = this.system.siteManager.getCurrentChild();
			activeNode = activeNode ? activeNode : this.system.siteManager.getDefaultNode();
			
			
			if (activeNode) {
				curCategory = getCategoryByNodeId(activeNode.getId());
				
				if (curCategory) {					
					mcCategory.updateByNodeId(activeNode.getId()); 
										
					curQuestion = 0;
					arrQuestion = curCategory.questions;					
					
					showQuestion();
				} 				
			}
		}
		
		private function onClickNextQuestionHandler(evt:MouseEvent):void {				
			curQuestion++;
			showQuestion();			
		}
		
		private function showQuestion():void {
			
			if (curQuestion < arrQuestion.length) {
				questionData = arrQuestion[curQuestion];
				curAnswer = null;
				
				while (mcAnswer.numChildren) {
					mcAnswer.removeChildAt(0);
				}
				
				var answerItem:MovieClip;
				var ypos:Number = 0;
				
				for (var i:int = 0; i < questionData.answers.length; i ++) {
					answerItem = new AnswserItem_FC();					
					answerItem.name = i.toString();
					answerItem.txtAnswer.autoSize = TextFieldAutoSize.LEFT;
					answerItem.txtAnswer.text = questionData.answers[i].toString();
					answerItem.txtAnswer.visible = false;
					answerItem.buttonMode = true;
					answerItem.mouseChildren = false;
					answerItem.addEventListener(MouseEvent.CLICK, onClickAnswerHandler);
					answerItem.txtLabel.text = (String.fromCharCode(i + 65));
					answerItem.y = ypos;
					
					mcAnswer.addChild(answerItem);
					ypos += answerItem.height + 10;
				}
				
				btnValidate.alpha = 0.75;
				btnValidate.visible = true;
				btnValidate.mouseEnabled = false;
				btnNext.y = btnValidate.y = mcAnswer.y + ypos + 30;
				
				btnNext.visible = false;
				
				loadImage(questionData.image);			
				
				if (!mp3_FC) {
					mp3_FC = new MP3_FC();
					mp3_FC.x = mcAnswer.x;
					mp3_FC.y = mcImage.y;
					addChild(mp3_FC);
				}				
				mp3_FC.playMP3(questionData.mp3);
			} 
			
			else {
					
				var index:int = arrCategory.indexOf(curCategory);
				index ++;
				if (index >= arrCategory.length) {
					index = 0;
				}
				
				this.system.siteManager.getChild(CategoryData(arrCategory[index]).nodeid).activate();
			}
		}
		
		private function onClickAnswerHandler(evt:MouseEvent):void {
			
			if (curAnswer) {
				curAnswer.gotoAndStop(1);
			}
			curAnswer = evt.currentTarget as MovieClip;
			curAnswer.gotoAndStop(2);
			
			btnValidate.alpha = 1;
			btnValidate.mouseEnabled = true;
		}
		
		private function onClickValidateAnswerHandler(evt:MouseEvent):void {
			
			btnValidate.visible = false;
			btnNext.visible = true;
			
			var answerItem:MovieClip;
			for (var i:int = 0; i < mcAnswer.numChildren; i ++) {
				answerItem = mcAnswer.getChildAt(i) as MovieClip;
				answerItem.mouseEnabled = false;
				answerItem.txtAnswer.visible = true;
				answerItem.removeEventListener(MouseEvent.CLICK, onClickAnswerHandler);				
				
				if (answerItem.name == questionData.right.toString()) {					
					if (curAnswer == answerItem) {
						answerItem.gotoAndStop(3);
					} else {
						curAnswer.gotoAndStop(4)
						answerItem.gotoAndStop(3);
					}
				}
			}
		}
		
			
		private function loadImage(url:String):void {
			ldrImage = new Loader();
			ldrImage.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteLoadImageHandler);
			ldrImage.load(new URLRequest(url));
			
			loading_FC.play();
			loading_FC.visible = true;
		}
		
		private function onCompleteLoadImageHandler(evt:Event):void {
			while (mcImage.mcHolder.numChildren) {
		
				mcImage.mcHolder.removeChildAt(0);
			}			
			
			var bmp:Bitmap = evt.currentTarget.content as Bitmap;
			bmp.smoothing = true;
			
			bmp.width = mcImage.mcBG.width;
			bmp.scaleY = bmp.scaleY;
			
			if (bmp.height > mcImage.mcBG.height) {
				bmp.height = mcImage.mcBG.height;
				bmp.scaleX = bmp.scaleY;
			}
			
			bmp.x = mcImage.mcBG.width / 2 - bmp.width / 2;
			bmp.y = mcImage.mcBG.height / 2 - bmp.height / 2;
			bmp.alpha = 0;
			
			mcImage.mcHolder.addChild(bmp);			
			TweenLite.to(bmp, 0.5, { alpha: 1 } );
			
			loading_FC.stop();
			loading_FC.visible = false;
		}
		
		override protected function dispose():void {
			this.system.siteManager.removeEventListener(NodeEvent.CHILD_CHANGE, onChildChangeHandler);			
		}
	}
}